package com.ccrc.simple.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ccrc.simple.entity.Role;
import com.ccrc.simple.service.IRoleService;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author 王成刚
 * @since 2017-08-03
 */
@Controller
@RequestMapping
public class RoleController extends BaseController {
	
	@Autowired
	private IRoleService roleService;
	
	@RequestMapping("/role")
    public ModelAndView index(ModelAndView modelAndView) {
        modelAndView.setViewName("role");
        modelAndView.addObject("roleList", roleService.selectList(null));
        return modelAndView;
    }
	
	@RequestMapping("/preSaveRole")
    public ModelAndView preSave(ModelAndView modelAndView, @RequestParam(value = "id", required = false) Long id) {
        modelAndView.setViewName("save_role");
        if (id != null) {
            modelAndView.addObject("user", roleService.selectById(id));
        }
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("saveRole")
    public Object save(Role role) {
        if (role.getId() == null) {
            return roleService.insert(role) ? renderSuccess("添加成功") : renderError("添加失败");
        } else {
            return roleService.updateById(role) ? renderSuccess("修改成功") : renderError("修改失败");
        }
    }

    @ResponseBody
    @RequestMapping("/deleteRole")
    public Object delete(@RequestParam(value = "id", required = false) Long id) {
        return roleService.deleteById(id) ? renderSuccess("删除成功") : renderError("删除失败");
    }
}
